package com.example.XMLparser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

//SAX parser to parse product 

public class XMLParser extends DefaultHandler
{
      
          
         public List<XmlValuesModel> list=null;
          
         // string builder acts as a buffer
         StringBuilder builder;
      
         XmlValuesModel productsValues=null;
          
          
          // Initialize the arraylist
          // @throws SAXException
         
         
         
           
         @Override
         public void startDocument() throws SAXException {
              
             /******* Create ArrayList To Store XmlValuesModel object ******/
             list = new ArrayList<XmlValuesModel>();
         }
      
          
          // Initialize the temp XmlValuesModel object which will hold the parsed info
          // and the string builder that will store the read characters
          // @param uri
          // @param localName ( Parsed Node name will come in localName  )
          // @param qName
          // @param attributes
          // @throws SAXException
           
         @Override
         public void startElement(String uri, String localName, String qName,
                 Attributes attributes) throws SAXException {
              
             /****  When New XML Node initiating to parse this function called *****/
              
             // Create StringBuilder object to store xml node value
             builder=new StringBuilder();
           /*   
             if(localName.equals("login")){
                  
                 //Log.i("parse","====login=====");
             }
             else if(localName.equals("status")){
                  
                 // Log.i("parse","====status=====");
             }
             else*/
             if(localName.equals("products")){
                  
             }
             else if(localName.equals("product")){
                  
                 // Log.i("parse","----Product start----");
                 /********** Create Model Object  *********/
                 productsValues = new XmlValuesModel();
             }
         }
          
          
          
          // Finished reading the login tag, add it to arraylist
          // @param uri
          // @param localName
          // @param qName
          // @throws SAXException
           
         @Override
         public void endElement(String uri, String localName, String qName)
                 throws SAXException {
             
              
             if(localName.equals("product")){

                 /** finished reading a product xml node, add it to the arraylist **/
                 list.add( productsValues );
                  
             }
             else  if(localName.equalsIgnoreCase("status")){
                  
                  
             }
             else  if(localName.equalsIgnoreCase("id")){  

            	 productsValues.setid(Integer.parseInt(builder.toString()));
             }
             else if(localName.equalsIgnoreCase("companyid")){

            	 productsValues.setcompanyid(Integer.parseInt(builder.toString()));
             }
             else if(localName.equalsIgnoreCase("company")){
            	 productsValues.setcompany(builder.toString());
             }
             else if(localName.equalsIgnoreCase("address")){
            	 productsValues.setaddress(builder.toString());
             }
             else if(localName.equalsIgnoreCase("city")){
            	 productsValues.setcity(builder.toString());
             }
             else if(localName.equalsIgnoreCase("state")){
            	 productsValues.setstate(builder.toString());
             }
             else if(localName.equalsIgnoreCase("zipcode")){
            	 productsValues.setzipcode(builder.toString());
             }
             else if(localName.equalsIgnoreCase("country")){
            	 productsValues.setcountry(builder.toString());
             }
             else if(localName.equalsIgnoreCase("telephone")){
            	 productsValues.settelephone(builder.toString());
             }
             else if(localName.equalsIgnoreCase("date")){
            	 productsValues.setdate(builder.toString());
             }
             else if(localName.equalsIgnoreCase("price")){
            	 productsValues.setPrice(builder.toString());
             }
             else if(localName.equalsIgnoreCase("description")){
            	 productsValues.setDescription(builder.toString());
             }
              
          // Log.i("parse",localName.toString()+"========="+builder.toString());
         }
      
         
          // Read the value of each xml NODE
          // @param ch
          // @param start
          // @param length
          // @throws SAXException
           
         @Override
         public void characters(char[] ch, int start, int length)
                 throws SAXException {
                  
             /******  Read the characters and append them to the buffer  ******/
             String tempString=new String(ch, start, length);
              builder.append(tempString);
         }
}